// bfs
class Solution {
    typedef pair<int, int> PII;
    int dx[4] = { 0,0,1,-1 };
    int dy[4] = { 1,-1,0,0 };
public:
    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) {
        if (image[sr][sc] == color)return image;
        int prev = image[sr][sc];
        queue<PII> q;
        q.push({ sr,sc });
        while (q.size())
        {
            auto [x, y] = q.front();
            q.pop();
            for (int i = 0; i < 4; i++)
            {
                int x1 = x + dx[i];
                int y1 = y + dy[i];
                if (x1 >= 0 && y1 >= 0 && x1 < image.size() && y1 < image[x1].size() && image[x1][y1] == prev)
                    q.push({ x1,y1 });
            }
            image[x][y] = color;
        }
        return image;
    }
};

class Solution {
    int ret = 0;
    typedef pair<int, int> PII;
    queue<PII> q;
public:
    int numIslands(vector<vector<char>>& grid) {

        for (int i = 0; i < grid.size(); i++)
        {
            for (int j = 0; j < grid[i].size(); j++)
            {
                if (grid[i][j] == '1')
                {
                    grid[i][j] = '0';
                    q.push({ i,j });
                    bfs(grid);
                    ret++;
                }
            }
        }
        return ret;
    }
    int dx[4] = { 0,0,1,-1 };
    int dy[4] = { 1,-1,0,0 };
    void bfs(vector<vector<char>>& grid)
    {
        while (!q.empty())
        {
            auto [x, y] = q.front();
            q.pop();
            for (int i = 0; i < 4; i++)
            {
                int a = x + dx[i];
                int b = y + dy[i];
                if (a >= 0 && b >= 0 && a < grid.size() && b < grid[x].size() && grid[a][b] == '1')
                {
                    grid[a][b] = '0';
                    q.push({ a,b });
                }
            }
        }
    }

};

class Solution {
    int sum = 0;
    typedef pair<int, int> PII;
    queue<PII> q;
    int dx[4] = { 0,0,1,-1 };
    int dy[4] = { 1,-1,0,0 };
public:
    int maxAreaOfIsland(vector<vector<int>>& grid) {
        int ret = 0;
        for (int i = 0; i < grid.size(); i++)
        {
            for (int j = 0; j < grid[i].size(); j++)
            {
                if (grid[i][j] == 1)
                {
                    q.push({ i,j });
                    grid[i][j] = 0;
                    dfs(grid);
                    ret = max(sum, ret);
                }
            }
        }
        return ret;
    }
    void dfs(vector<vector<int>>& grid)
    {
        sum = 1;
        while (q.size())
        {
            auto [a, b] = q.front();
            q.pop();
            for (int i = 0; i < 4; i++)
            {
                int x = a + dx[i];
                int y = b + dy[i];
                if (x >= 0 && y >= 0 && x < grid.size() && y < grid[x].size() && grid[x][y] == 1)
                {
                    grid[x][y] = 0;
                    q.push({ x,y });
                    sum++;
                }
            }
        }
    }
};


class Solution {
    typedef pair<int, int> PII;
    queue<PII> q;
    bool check[201][201];
    int dx[4] = { 0,0,1,-1 };
    int dy[4] = { 1,-1,0,0 };
public:
    void solve(vector<vector<char>>& board) {

        if (board.size() < 2 || board[0].size() < 2)
            return;
        int m = board.size();
        int n = board[0].size();
        for (int i = 0; i < m; i++)
        {
            if (board[i][0] == 'O')
            {
                check[i][0] = true;
                q.push({ i,0 });
                bfs(board);
            }
        }
        for (int i = 0; i < m; i++)
        {
            if (board[i][n - 1] == 'O')
            {
                check[i][n - 1] = true;
                q.push({ i,n - 1 });
                bfs(board);
            }
        }
        for (int i = 0; i < n; i++)
        {
            if (board[0][i] == 'O')
            {
                check[0][i] = true;
                q.push({ 0,i });
                bfs(board);
            }
        }
        for (int i = 0; i < n; i++)
        {
            if (board[m - 1][i] == 'O')
            {
                check[m - 1][i] = true;
                q.push({ m - 1,i });
                bfs(board);
            }
        }
        for (int i = 1; i < m - 1; i++)
        {
            for (int j = 1; j < n - 1; j++)
            {
                if (check[i][j] == false && board[i][j] == 'O' && check[i][j] == false)
                {
                    board[i][j] = 'X';
                }
            }
        }
    }
    void bfs(vector<vector<char>>& board)
    {
        while (q.size())
        {
            auto [x, y] = q.front();
            q.pop();
            for (int i = 0; i < 4; i++)
            {
                int a = x + dx[i];
                int b = y + dy[i];
                if (a >= 0 && b >= 0 && a < board.size() && b < board[a].size() && check[a][b] == false && board[a][b] == 'O')
                {
                    check[a][b] = true;
                    q.push({ a,b });
                }
            }
        }
    }
};